import { Reducer, Effect, Subscription } from 'umi';
import {
  getGradeSummary,
  getCetdetial,
  getSecondClassDetail,
  getGradeSummaryScoreDetial,
  getStudentAnalysis,
  getSchoolPrecaution,
  getPrecautionDetail,
  getOutclassSummary,
  getMentorMasterOtherScore,
  getMentorMasterScoreList,
  getPrecautionAnalysis,
  getSecondClassDetailForStu,
  getStudentClassTwoBystudentCode,
  getMentorOtherScore,
  getMentorScoreList,
} from '@/services/StudentAnalysisStudent';

import { useCallback } from 'react';
/*
-----------------------------------------------------------------------------------------------------------------
本model用来演示正规的流程，即
①组件连接仓库；
②组件通过connect连接后给组件自动传的参数dispatch访问仓库中的异步函数effects；
③effects获取到数据后调用同步函数reducers存储数据到仓库state中
④组件自动更新参数并重新渲染
-----------------------------------------------------------------------------------------------------------------
 */

/*
-----------------------------------------------------------------------------------------------------------------
ts规范：对变量进行声明
 */
//对state声明，state用于仓库存储数据，也就是说state就是仓库
export type StateType = {
  list?: any[];
  studentScoreList?: any[];
};
//对model声明
export type StudentAnalysisStudentModelType = {
  namespace: string;
  state: StateType;
  effects: {
    getGradeSummaryRankEffects: Effect; //学生获取学生成绩汇总
    getGradeSummaryEnglishEffects: Effect; //学生获取学生四六级成绩汇总
    getGradeSummarySportEffects: Effect; //学生获取学生体测成绩汇总
    getGradeSummaryClassTwoEffects: Effect; //学生获取学生二课成绩汇总
    getGradeSummaryScoreDetialEffects: Effect; //学生获取学生课程成绩
    getStudentAnalysisEffects: Effect; //学生个人成绩分析数据
    getStudentRankAnalysisEffects: Effect; //学生个人排名分析数据
    getStudentClassTwoEffect: Effect; //学生个人二课总成绩
    getSecondClassDetailEffects: Effect; //学生获取学生二课成绩
    getStudentClassTwoAnalysisEffect: Effect; //学生个人二课成绩分析（目前为辅导员看到的学生二课界面）
    getCetdetialEffects: Effect; //辅导员获取学生四六级成绩
    getSchoolPrecautionEffects: Effect; //辅导员获取学生预警信息
    getPrecautionDetailEffects: Effect; //辅导员获取预警学生的详细信息
    getOutclassSummaryEffects: Effect; //辅导员获取所有学生的二课成绩
    getStudentAnalysisFamilyEffects: Effect; //家长获取学生成绩汇总
    getMentorMasterScoreListEffects: Effect; //育人团队带班成绩列表
    getMentorMasterOtherScoreEffects: Effect; //育人团队获取其他成绩列表
    getPrecautionAnalysisEffects: Effect; //辅导员获取横向预警分析
    getStudentOtherScoreEffect: Effect; //班导师获取学生其他成绩
    getStudentScoreEffect: Effect; //班导师获取学生一课成绩
  };
  reducers: {};
  subscriptions: {
    // setup: Subscription;
  };
};
/*
ts规范：对变量进行声明
-----------------------------------------------------------------------------------------------------------------
 */

/*
-----------------------------------------------------------------------------------------------------------------
model结构：namespace，state（仓库，用来存储数据），
effects异步处理，reducers同步操作（用于接收effects的结果并存到state中），subscription订阅（用于监听页面的跳转）
state变化后，连接此model的组件所接收到的参数会自动渲染
 */
const StudentAnalysisStudentModel: StudentAnalysisStudentModelType = {
  //本model唯一标识，命名标准：不能有'-'，可以有'_'，可以有大写
  //个人标准：以后都以'Model_'开头
  namespace: 'Model_StudentAnalysisStudentModel', //里面存的是state
  state: {
    list: [],
    studentScoreList: [],
  },
  //命名规范（个人）：函数功能+FromServices+Effects
  effects: {
    //获取单个学生成绩汇总
    *getGradeSummaryRankEffects(
      { payload }: any,
      { call, put }: any,
    ): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(getGradeSummary, token, payload.data);
      let data = [
        {
          key: 0,
          stuName: result.data.data.courseGrade.stuName,
          stuNumber: result.data.data.courseGrade.stuNumber,
          creditRequired: result.data.data.courseGrade.creditRequired,
          creditEarned: result.data.data.courseGrade.creditEarned,
          gpa: result.data.data.courseGrade.gpa,
          classRank: result.data.data.courseGrade.classRank,
          majorRank: result.data.data.courseGrade.majorRank,
        },
      ];

      payload.callback(data);
    },
    //获取单个学生四六级成绩汇总
    *getGradeSummaryEnglishEffects(
      { payload }: any,
      { call, put }: any,
    ): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(getGradeSummary, token, payload.data);
      let data = [
        {
          key: 1,
          fourLevel: result.data.data.cetGrades.fourLevel,
          sixLevel: result.data.data.cetGrades.sixLevel,
        },
      ];
      payload.callback(data);
    },
    //获取单个学生成绩体测汇总
    *getGradeSummarySportEffects(
      { payload }: any,
      { call, put }: any,
    ): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(getGradeSummary, token, payload.data);
      let data = [
        {
          key: 3,
          firstYear: result.data.data.sportsGrades.firstYear,
          secondYear: result.data.data.sportsGrades.secondYear,
          thirdYear: result.data.data.sportsGrades.thirdYear,
          fourthYear: result.data.data.sportsGrades.fourthYear,
        },
      ];
      payload.callback(data);
    },
    //获取单个学生成绩二课汇总
    *getGradeSummaryClassTwoEffects(
      { payload }: any,
      { call, put }: any,
    ): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(getGradeSummary, token, payload.data);
      let data = [
        {
          key: 2,
          nationalLevel: result.data.data.secondaryGrades.nationalLevel,
          provincialLevel: result.data.data.secondaryGrades.provincialLevel,
          universityLevel: result.data.data.secondaryGrades.universityLevel,
          instituteLevel: result.data.data.secondaryGrades.instituteLevel,
          totalScore: result.data.data.secondaryGrades.totalScore,
        },
      ];
      payload.callback(data);
    },
    //获取单个学生所有课程成绩
    *getGradeSummaryScoreDetialEffects(
      { payload }: any,
      { call, put }: any,
    ): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(
        getGradeSummaryScoreDetial,
        token,
        payload.data,
      );
      payload.callback(result.data.data);
    },
    //获取单个学生成绩分析
    *getStudentAnalysisEffects(
      { payload }: any,
      { call, put }: any,
    ): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(getStudentAnalysis, token, payload.data);
      let list = [
        {
          year: '1-1',
          value: result.data.data.gpaCompare.classGpa11,
          category: '班级平均成绩',
        },
        {
          year: '1-2',
          value: result.data.data.gpaCompare.classGpa12,
          category: '班级平均成绩',
        },
        {
          year: '2-1',
          value: result.data.data.gpaCompare.classGpa21,
          category: '班级平均成绩',
        },
        {
          year: '2-2',
          value: result.data.data.gpaCompare.classGpa22,
          category: '班级平均成绩',
        },
        {
          year: '3-1',
          value: result.data.data.gpaCompare.classGpa31,
          category: '班级平均成绩',
        },
        {
          year: '3-2',
          value: result.data.data.gpaCompare.classGpa32,
          category: '班级平均成绩',
        },
        {
          year: '4-1',
          value: result.data.data.gpaCompare.classGpa41,
          category: '班级平均成绩',
        },
        {
          year: '4-2',
          value: result.data.data.gpaCompare.classGpa42,
          category: '班级平均成绩',
        },
        {
          year: '1-1',
          value: result.data.data.gpaCompare.gpa11,
          category: '个人成绩',
        },
        {
          year: '1-2',
          value: result.data.data.gpaCompare.gpa12,
          category: '个人成绩',
        },
        {
          year: '2-1',
          value: result.data.data.gpaCompare.gpa21,
          category: '个人成绩',
        },
        {
          year: '2-2',
          value: result.data.data.gpaCompare.gpa22,
          category: '个人成绩',
        },
        {
          year: '3-1',
          value: result.data.data.gpaCompare.gpa31,
          category: '个人成绩',
        },
        {
          year: '3-2',
          value: result.data.data.gpaCompare.gpa32,
          category: '个人成绩',
        },
        {
          year: '4-1',
          value: result.data.data.gpaCompare.gpa41,
          category: '个人成绩',
        },
        {
          year: '4-2',
          value: result.data.data.gpaCompare.gpa42,
          category: '个人成绩',
        },
      ];
      payload.callback(list);
    },
    //学生个人排名分析数据
    *getStudentRankAnalysisEffects(
      { payload }: any,
      { call, put }: any,
    ): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(getStudentAnalysis, token, payload.data);
      let list = [
        {
          year: '1-1',
          value: result.data.data.gpaCompare.gpa11,
          category: '个人成绩',
        },
        {
          year: '1-2',
          value: result.data.data.gpaCompare.gpa12,
          category: '个人成绩',
        },
        {
          year: '2-1',
          value: result.data.data.gpaCompare.gpa21,
          category: '个人成绩',
        },
        {
          year: '2-2',
          value: result.data.data.gpaCompare.gpa22,
          category: '个人成绩',
        },
        {
          year: '3-1',
          value: result.data.data.gpaCompare.gpa31,
          category: '个人成绩',
        },
        {
          year: '3-2',
          value: result.data.data.gpaCompare.gpa32,
          category: '个人成绩',
        },
        {
          year: '4-1',
          value: result.data.data.gpaCompare.gpa41,
          category: '个人成绩',
        },
        {
          year: '4-2',
          value: result.data.data.gpaCompare.gpa42,
          category: '个人成绩',
        },
      ];
      payload.callback(list);
    },
    //获取单个学生二课成绩
    *getStudentClassTwoEffect({ payload }: any, { call, put }: any): Generator {
      let token = localStorage.getItem('token');
      let list = {
        code: 200,
        msg: '查找成功！',
        data: {
          studentCode: '194765',
          studentName: '余霖辉',
          sex: '男',
          enrollment: '201909',
          programme: 4,
          mentorName: '李华',
          overallGrade: 31.11,
          classRank: 1,
          majorRank: 6,
          dimensionOne: {
            idealsBeliefs: 7.9,
            practicalService: 7.0,
            literatureArt: 0.0,
            academicTechnology: 9.18,
            socialWork: 1.1,
            physicalFitness: 1.4,
          },
          dimensionTwo: {
            activitiesCompetitions: 26.13,
            awardsHonors: 6.48,
            studentOrganization: 3.0,
          },
          dimensionThree: {
            collegeLevel: 9.05,
            universityLevel: 12.16,
            provincialLevel: 4.4,
            nationalLevel: 4.0,
          },
          totalGrade: 87.3,
        },
      };
      const result: any = yield call(
        getSecondClassDetailForStu,
        token,
        payload.value,
      );
      console.log(result.data.data);
      payload.callback(result.data.data);
    },
    //获取单个学生二课成绩分析
    *getStudentClassTwoAnalysisEffect(
      { payload }: any,
      { call, put }: any,
    ): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(
        getStudentClassTwoBystudentCode,
        token,
        payload.value,
      );
      let list = [
        {
          item: '理想信念',
          user: result.data.data.studentName,
          score: Number(
            result.data.data.semester.第一学年秋季学期.dimensionOne
              .idealsBeliefs,
          ),
        },
        {
          item: '实践服务',
          user: result.data.data.studentName,
          score: Number(
            result.data.data.semester.第一学年秋季学期.dimensionOne
              .practicalService,
          ),
        },
        {
          item: '文化艺术',
          user: result.data.data.studentName,
          score: Number(
            result.data.data.semester.第一学年秋季学期.dimensionOne
              .literatureArt,
          ),
        },
        {
          item: '学术科技',
          user: result.data.data.studentName,
          score: Number(
            result.data.data.semester.第一学年秋季学期.dimensionOne
              .academicTechnology,
          ),
        },
        {
          item: '社会工作',
          user: result.data.data.studentName,
          score: Number(
            result.data.data.semester.第一学年秋季学期.dimensionOne.socialWork,
          ),
        },
        {
          item: '体育素质',
          user: result.data.data.studentName,
          score: Number(
            result.data.data.semester.第一学年秋季学期.dimensionOne
              .physicalFitness,
          ),
        },
      ];
      payload.callback(list);
    },
    //辅导员获取学生四六级成绩
    *getCetdetialEffects({ payload }: any, { call, put }: any): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(getCetdetial, token, payload.studentCode);
      // 将 isPass4 和 isPass6 进行转换
      const transformedData = result.data.data.map(
        (item: any, index: number) => ({
          ...item,
          isPass4: item.isPass4 === 1 ? '是' : '否',
          isPass6: item.isPass6 === 1 ? '是' : '否',
          order: index + 1, // 从 1 开始递增
        }),
      );
      payload.callback(transformedData);
    },
    //辅导员获取学生预警信息
    *getSchoolPrecautionEffects(
      { payload }: any,
      { call, put }: any,
    ): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(
        getSchoolPrecaution,
        token,
        payload.teacherCode,
      );
      payload.callback(result.data.data);
    },
    //学生获取学生二课成绩
    *getSecondClassDetailEffects(
      { payload }: any,
      { call, put }: any,
    ): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(
        getSecondClassDetail,
        token,
        payload.studentCode,
      );
      payload.callback(result.data.data);
    },
    //辅导员获取预警学生的详细信息
    *getPrecautionDetailEffects(
      { payload }: any,
      { call, put }: any,
    ): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(
        getPrecautionDetail,
        token,
        payload.studentCode,
      );
      payload.callback(result.data.data);
    },
    //辅导员获取横向预警分析
    *getPrecautionAnalysisEffects(
      { payload }: any,
      { call, put }: any,
    ): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(
        getPrecautionAnalysis,
        token,
        payload.studentCode,
      );
      payload.callback(result.data.data);
    },
    //辅导员获取所有学生的二课成绩
    *getOutclassSummaryEffects(
      { payload }: any,
      { call, put }: any,
    ): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(getOutclassSummary, token, payload);
      const transformedData = result.data.data.map(
        (item: any, index: number) => ({
          ...item,
          order: index + 1, // 从 1 开始递增
        }),
      );
      payload.callback(transformedData);
    },
    //家长获取学生成绩汇总
    *getStudentAnalysisFamilyEffects(
      { payload }: any,
      { call, put }: any,
    ): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(getStudentAnalysis, token, payload.data);
      let list = [
        {
          stuName: result.data.data.gpaCompare.stuName,
          className: result.data.data.gpaCompare.className,
          majorName: result.data.data.gpaCompare.majorName,
          stuNumber: result.data.data.gpaCompare.stuNumber,
        },
      ];
      payload.callback(list);
    },
    //育人团队带班成绩列表
    *getMentorMasterScoreListEffects(
      { payload }: any,
      { call, put }: any,
    ): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(
        getMentorMasterScoreList,
        token,
        payload.data,
      );
      const transformedData = result.data.data.map(
        (item: any, index: number) => ({
          ...item,
          order: index + 1, // 从 1 开始递增
        }),
      );
      payload.callback(transformedData);
    },
    //育人团队获取其他成绩列表
    *getMentorMasterOtherScoreEffects(
      { payload }: any,
      { call, put }: any,
    ): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(
        getMentorMasterOtherScore,
        token,
        payload.data,
      );
      const transformedData = result.data.data.map(
        (item: any, index: number) => ({
          ...item,
          order: index + 1, // 从 1 开始递增
        }),
      );
      payload.callback(transformedData);
    },
    //班导师获取其他成绩列表
    *getStudentOtherScoreEffect(
      { payload }: any,
      { call, put }: any,
    ): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(getMentorOtherScore, token, payload.data);
      const transformedData = result.data.data.map(
        (item: any, index: number) => ({
          ...item,
          id: index + 1, // 从 1 开始递增
        }),
      );
      payload.callback(transformedData);
    },
    //班导师获取学生一课成绩
    *getStudentScoreEffect({ payload }: any, { call, put }: any): Generator {
      let token = localStorage.getItem('token');
      const result: any = yield call(getMentorScoreList, token, payload.data);
      const transformedData = result.data.data.map(
        (item: any, index: number) => ({
          ...item,
          id: index + 1, // 从 1 开始递增
        }),
      );
      payload.callback(transformedData);
    },
  },

  //命名规范（个人）：函数功能+Reducers
  reducers: {},

  subscriptions: {},
};
/*
model结构
-----------------------------------------------------------------------------------------------------------------
 */

export default StudentAnalysisStudentModel;
